<?php

namespace app\admin\model;

use think\Model;
use think\config;

class VGamerebate extends Model
{
    // 表名
    protected $name = 'v_gamerebate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'site_data_text',
        'type_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getSiteDataList()
    {
       $game_list = Config::get('GameName');
	   unset($game_list['0']);
       return $game_list;	  
    }     

    public function getTypeList()
    {
        return ['free' => __('Free'),'quota' => __('Quota'),'proportion' => __('Proportion')];
    }     

    public function getStatusList()
    {
        return ['Geton' => __('Geton'),'Notstart' => __('Notstart'),'Failure' => __('Failure')];
    }     


    public function getSiteDataTextAttr($value, $data)
    {
        $value = $value ? $value : $data['site_data'];
        $valueArr = explode(',', $value);
        $list = $this->getSiteDataList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setSiteDataAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
